package com.si.sea_island.controller;

import com.si.sea_island.base.JsonResult;
import com.si.sea_island.base.ResultCode;
import com.si.sea_island.mapper.SmsDBMapper;
import com.si.sea_island.mapper.UserDBMapper;
import com.si.sea_island.pojo.sms.SmsDB;
import com.si.sea_island.utils.SmsUtils;
import com.si.sea_island.utils.WPhoneUtils;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@Transactional//开启事务回滚
@Api(tags = "短信模块")
public class SmsController {

    //短信验证码
    @Autowired
    SmsDBMapper smsDBMapper;
    //会员
    @Autowired
    UserDBMapper userDBMapper;

    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone",value = "手机号",required = true,paramType = "param"),
    })
    @ApiOperation(value = "发送短信", notes = "发送短信")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功", response = String.class)
    })
    @PostMapping("/api/getSMS")
    public Map<String, Object> getSMS(HttpServletRequest request) {
        //检测请求参数
        String phone = request.getParameter("phone");
        //检验手机号
        if (!WPhoneUtils.isChinaPhone(phone)) {
            return JsonResult.error(ResultCode.phone_format_error,"请填写正确的手机号");
        }
        //用户校验

        SmsDB ordSmsDB = smsDBMapper.getSmsByPhone(phone);
        if (ordSmsDB != null) {
            System.out.println("getSMS--"+"--"+"phone--"+phone+"--savaCode--"+ordSmsDB.getCode());
        } else {
            System.out.println("getSMS--"+"--"+"phone--"+phone);
        }
        String savaCode = SmsUtils.getRandomSMS();//短信
        boolean result = false;
        if (ordSmsDB != null) {
            //设置新短信
            ordSmsDB.setCode(savaCode);
            ordSmsDB.setTime(String.valueOf(System.currentTimeMillis()));
            int sccuess = smsDBMapper.updateSmsDB(ordSmsDB);
            if (sccuess == 0) {
                return JsonResult.error(ResultCode.service_error,"服务器繁忙，稍后请重试");
            }
        } else {
            SmsDB smsDB = new SmsDB();
            smsDB.setPhone(phone);
            smsDB.setCode(savaCode);
            smsDB.setTime(String.valueOf(System.currentTimeMillis()));
            int sccuess = smsDBMapper.insertSmsDB(smsDB);
            if (sccuess == 0) {
                return JsonResult.error(ResultCode.sms_not_send,"验证码发送失败，稍后请重试");
            }
        }
        try{
            result = SmsUtils.sendSMS(phone,savaCode);
        }catch (Exception e){
            return JsonResult.error(ResultCode.sms_already_send,"验证码发送失败，请重试!");
        }
//        result=true;
        System.out.println("getSMS--"+"result--"+result+"--"+"phone--"+phone+"--savaCode--"+savaCode);
        if (result) {
            return JsonResult.success("验证码已发送，请注意查收!");
        } else {
            return JsonResult.error(ResultCode.sms_already_send,"验证码发送失败，请重试!");
        }
    }


}
